layui.use(['table', 'jquery'], function () {
    var table = layui.table,
        $ = layui.jquery;

    findAll();

    function findAll() {

        let code = $('#code').val();
        let name = $('#name').val();
        let leader = $('#leader').val();
        let params = {
            code: code,
            name: name,
            leader: leader
        };

        let cols = [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'id', width: 60, title: 'ID', sort: true, fixed: 'left'}
            , {field: 'code', width: '15%', title: '小组编号'}
            , {field: 'name', width: '15%', title: '小组名称'}
            , {field: 'coach', width: '10%', title: '教练'}
            , {field: 'headMaster', width: '10%', title: '班主任'}
            , {
                field: 'leader', width: 100, title: '小组长',
                templet: e => {
                    if (e.leader != null && e.leader !== undefined && e.leader !== '') {
                        return e.leader;
                    } else {
                        return '暂未指定';
                    }
                }
            }
            , {
                field: 'number', width: 125, title: '小组人数', sort: true, templet: e => {
                    if (e.number != null && e.number !== undefined && e.number !== '') {
                        return e.number;
                    } else {
                        return 0;
                    }
                }
            }
            , {field: 'createTime', width: '15%', title: '创建时间', sort: true}
            , {field: 'updateTime', width: '15%', title: '修改时间', sort: true}
            , {fixed: 'right', width: 285, align: 'center', toolbar: '#operating'}
        ]];


        table.render({
            elem: '#dataList'
            , url: LOCAL_CROSS_URL + "/team/findAll"
            , where: params
            , method: 'get'
            , headers: HEADERS
            , request: REQUEST
            , response: RESPONSE
            , cellMinWidth: 80
            , height: 720
            , toolbar: '#toolbar'
            , skin: 'line'
            , even: true
            , page: true
            , limits: [5, 10, 15, 30]
            , limit: 10
            , title: '小组数据表'
            , cols: cols

        });
    }

    //监听头工具栏事件
    table.on('toolbar(dataList)', function (obj) {
        const checkStatus = table.checkStatus(obj.config.id)
            , data = checkStatus.data;
        switch (obj.event) {
            case 'add':
                addTeam();
                break;
            case 'show':
                if (data.length === 0) {
                    layer.msg('请选择一行');
                } else if (data.length > 1) {
                    layer.msg('只能同时查看一个');
                } else {
                    showMembers(checkStatus.data[0].code);
                }
                break;
        }
    });

    //监听行工具事件
    table.on('tool(dataList)', function (obj) {
        const data = obj.data
            , layEvent = obj.event;
        if (layEvent === 'distribute') {
            //分配组员
            distributeMembers(data.code);
        } else if (layEvent === 'teamLeader') {
            //分配组长
            distributeLeader(data.id, data.code);
        } else if (layEvent === 'del') {
            layer.confirm('真的删除行么', function (index) {
                layer.close(index);
                deleteTeam(data.id);
            });
        } else if (layEvent === 'edit') {
            updateTeam(data.id);
        }
    });


    /**
     * 添加
     */
    function addTeam() {
        commonLayerOpen('新增小组', 'teamAdd.html');
    }

    /**
     * 删除
     * @param id
     */
    function deleteTeam(id) {

        let result = commonAjax('/team/delete', {id: id}, 'get');
        if (result.count === 200) {
            layerMsgDeleteSuccess();
            //刷新
            findAll();
        } else {
            layerMsgDeleteFail();
        }
    }


    /**
     * 修改
     * @param id
     */
    function updateTeam(id) {
        sessionStorage.setItem("teamId", id);
        commonLayerOpen('小组详情', 'teamDetails.html');
    }

    /**
     * 查询
     */
    $('#query').on('click', function () {
        findAll();
    });

    /**
     * 重置
     */
    $('#reset').on('click', function () {
        $('#code').val('');
        $('#name').val('');
        $('#leader').val('');
    });

    /**
     * 分配组员
     */
    function distributeMembers(code) {
        sessionStorage.setItem("teamStudentCode", code);
        layer.open({
            title: '分配组员',
            area: ["600px", "500px"],
            shadeClose: true,
            anim: 2,
            type: 2,
            content: 'teamDistributeMembers.html'
        });
    }

    /**
     * 分配组长
     * @param id
     * @param code
     */
    function distributeLeader(id, code) {
        sessionStorage.setItem("teamLeaderId", id);
        sessionStorage.setItem("teamCode", code);
        commonLayerOpen("分配组长", "teamDistributeLeader.html")
    }

    /**
     * 查看小组成员
     * @param code
     */
    function showMembers(code) {
        sessionStorage.setItem("teamMemberCode", code);
        layer.open({
            title: '查看组员',
            //调整弹窗大小
            area: ["100%", "400px"],
            //点击旁边地方自动关闭
            shadeClose: true,
            anim: 2,
            type: 2,
            content: 'teamMembers.html',
        });
    }

});